package com.molichuxing.services.infrastructure.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.molichuxing.framework.exception.BizException;
import com.molichuxing.services.infrastructure.bean.CheckDisputeBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.CheckDisputeMapper;
import com.molichuxing.services.infrastructure.dto.request.create.CheckDisputeCreateDto;
import com.molichuxing.services.infrastructure.dto.request.modify.CheckDisputeModifyDto;
import com.molichuxing.services.infrastructure.dto.response.CheckDisputeDto;
import com.molichuxing.services.infrastructure.service.CheckDisputeService;

/**
 * <p>
 * 争议处理 服务实现类
 * </p>
 *
 * @author
 * @since 2019-08-15
 */
@Service("checkDisputeService")
public class CheckDisputeServiceImpl implements CheckDisputeService {

	private static final Logger log = Logger.getLogger(CheckDisputeServiceImpl.class);

	@Resource
	private CheckDisputeMapper checkDisputeMapper;

	/***
	 * 争议处理
	 * 
	 * @param checkId
	 * @param discussLossFee
	 * @param remark
	 * @param operatorAccount
	 *            操作人
	 * @return
	 */
	@Override
	public int modifyCheckDispute(Integer checkId, BigDecimal discussLossFee, String remark, String operatorAccount)
			throws Exception {
		return checkDisputeMapper.modifyCheckDispute(checkId, discussLossFee, remark, operatorAccount);
	}

	@Override
	public CheckDisputeDto getByCheckId(Integer checkId) {

		return Convert.toCheckDisputeDto(checkDisputeMapper.getByCheckId(checkId));
	}

	@Override
	public int createCheckDispute(CheckDisputeCreateDto checkDisputeCreateDto) {
		CheckDisputeBean checkDisputeBean = Convert.toCheckDisputeBean(checkDisputeCreateDto);
		return checkDisputeMapper.create(checkDisputeBean);
	}

	@Override
	public Boolean modifyCheckDispute(CheckDisputeModifyDto checkDisputeModifyDto) throws Exception {
		Integer id = checkDisputeModifyDto.getId();
		CheckDisputeBean checkDisputeBean = checkDisputeMapper.getById(id);
		if (null == checkDisputeBean) {
			throw new BizException("争议信息不存在");
		}

		Convert.toCheckDisputeBean(checkDisputeModifyDto, checkDisputeBean);
		return (checkDisputeMapper.modifyById(checkDisputeBean) > 0) ? true : false;
	}

	/**
	 * 查询
	 * 
	 * @param orderCodes
	 * @return
	 */
	@Override
	public Map<Long, CheckDisputeDto> getCheckDispute(List<Long> orderCodes) {
		if (orderCodes == null || orderCodes.isEmpty()) {
			return null;
		}
		 Map<Long, CheckDisputeDto> result = new HashMap<>();
		for (Long orderCode : orderCodes) {
			CheckDisputeBean bean = checkDisputeMapper.getCheckDisputeByOrderCode(orderCode);
			if (bean == null) {
				continue;
			} 
			result.put(orderCode, Convert.toCheckDisputeDto(bean)); 
		}
		return result;
	}

}
